

import 'package:baby/Entities/Store/CartItem.dart';
import 'package:baby/Entities/Store/ProductCategory.dart';
import 'package:baby/StateManagement/ModelProvider.dart';
import 'package:baby/StateManagement/ObservableList.dart';

import 'Store/Product.dart';

class DataContext with Model {
  MyObservableList<ProductCategory> productCategories = MyObservableList<ProductCategory>();
  MyObservableList<CartItem> cart = MyObservableList<CartItem>();

  /**
   * مال ھارۋىسىغا مال قوشۇش
   */
  void addToCart(CartItem cart) {
    var cartItem = this.cart.firstWhere((item) => item.product.guid == cart.product.guid, orElse: () => null);
    cartItem ??= (){
      var item = CartItem()
        ..product = cart.product
        ..count = 0.0;
      this.cart.add(item);
      this.cart.applyChanges();
      return item;
    }();

    cartItem.count += cart.count;
    cartItem.applyChanges();
  }

  /**
   * مال ھارۋىسىدىن چىقىرۋىتىش
   */
  void removeFromCart(CartItem cartItem) {
    var element = cart.singleWhere((_cartItem) => _cartItem.product.guid == cartItem.product.guid, orElse: () => null);
    if (element != null) {
      this.cart.remove(element);
      this.cart.applyChanges();
    }
  }
}
